package com.sdp.right.control;


import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.utils.UniqueCheckTools;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.db.sys_menu.service.ISysMenuService;
import com.sdp.right.db.sys_system.service.ISysSystemService;
import com.sdp.right.entity.SysSystem;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-10-16
 */
@RestController
@RequestMapping("/SysSystemController")
public class SysSystemController {

	@Autowired
	private ISysSystemService iSysSystemService;
	
	@Autowired
	private ISysMenuService iSysMenuService;
	
	@Autowired
	private Query query;
	
	@Autowired
	HttpServletRequest request;
	
	@Autowired
	private UniqueCheckTools uniqueCheckTools;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iSysSystemService, SysSystem.class, dataCenter));
	}
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody SysSystem sysSystem) {
		if(sysSystem==null) {
			return Result.error("业务系统不存在");
		}
		if(StringUtils.isEmpty(sysSystem.getKeyId())) {
			if(sysSystem.getSystemOrder()==null) {
				sysSystem.setSystemOrder(0);
			}
			Result<?> r = uniqueCheckTools.isUniqueSingle(iSysSystemService, SysSystem.class, new String[] {"systemNo","systemName"}, "keyId",sysSystem, null, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("业务系统编号和名称不能重复");
			}
			iSysSystemService.save(sysSystem);
			
			//添加默认系统菜单
			iSysMenuService.initSystemMenu(sysSystem);
			
			return Result.ok();
		}else {
			Result<?> r = uniqueCheckTools.isUniqueSingle(iSysSystemService, SysSystem.class, new String[] {"systemNo","systemName"}, "keyId",null, sysSystem, false);
			if(r==null || !r.isSuccess()) {
				return Result.error("业务系统编号和名称不能重复");
			}
			iSysSystemService.updateById(sysSystem);
			return Result.ok();
		}
	}
	
	@GetMapping(value = "/getSystemInfo")
	public Result<?> getSystemInfo() {
		return Result.ok(iSysSystemService.list());
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="systemNo",required=true) String systemNo) {
		if(StringUtils.isEmpty(systemNo)) {
			return Result.error("系统编码不存在,不能删除");
		}
		iSysSystemService.removeSystem(systemNo);
		
		return Result.ok();
	}
	
	
}

